package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.controller.dto.InspectAddrCountDto;
import com.yqg.protection.entity.RecordInspection;
import com.yqg.protection.mapper.RecordInspectionMapper;
import com.yqg.protection.service.IRecordInspectionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * RecordInspectionController 前端控制器
 *
 * @author 小宇
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/record-inspection")
public class RecordInspectionController {

    @Resource
    private IRecordInspectionService recordInspectionService;

    @Autowired
    private RecordInspectionMapper recordInspectionMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改巡检记录")
    public Result save(@RequestBody RecordInspection recordInspection) {
        recordInspection.setIsDelete(0);
        return Result.success(recordInspectionService.saveOrUpdate(recordInspection));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除单个巡检记录")
    public Result delete(@PathVariable Integer id) {
        RecordInspection recordInspection = recordInspectionService.getById(id);
        recordInspection.setIsDelete(1);
        return Result.success(recordInspectionService.saveOrUpdate(recordInspection));
    }

    /**
     * 删除列表
     */
    @PostMapping("/deleteByIdList")
    @SysLog("删除多个巡检记录")
    public Result deleteByIdList(@RequestBody List<Long> id) {
        List<RecordInspection> recordInspections = recordInspectionService.listByIds(id);
        for (RecordInspection recordInspection : recordInspections) {
            recordInspection.setIsDelete(1);
        }
        return Result.success(recordInspectionService.saveOrUpdateBatch(recordInspections));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll(Long address) {
        QueryWrapper<RecordInspection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.eq("address", address);
        queryWrapper.eq("state", "1");
        List<RecordInspection> list = recordInspectionService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(recordInspectionService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           String startTime,
                           String endTime,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<RecordInspection> recordInspections = recordInspectionMapper.findPage(pageNum, pageSize, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        Long count = recordInspectionMapper.findPageCount(keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", recordInspections);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord,
                       String startTime,
                       String endTime,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        recordInspectionService.export(response, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入巡检记录")
    public void importData(MultipartFile file) {
        recordInspectionService.importData(file);
    }

    /**
     * 分页查询地址巡检次数
     */
    @GetMapping("/findInCountGroupByAddr")
    public Result findInCountGroupByAddr(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         String keyWord,
                                         Integer level,
                                         Long organizationId,
                                         String isAllClient,
                                         @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<InspectAddrCountDto> inspectAddrCountDtos = recordInspectionMapper.findInGroupByAddr(pageNum, pageSize, keyWord, level, organizationId, isAllClient, clientIds);
        Long count = recordInspectionMapper.findInCountGroupByAddr(keyWord, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", inspectAddrCountDtos);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/getDataByAddr")
    public Result getDataByAddr(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         String keyWord,
                                         Long id) {
        pageNum = pageNum * pageSize - pageSize;
        List<RecordInspection> recordInspections = recordInspectionMapper.getDataByAddr(pageNum, pageSize, keyWord, id);
        return Result.success(recordInspections);
    }
}
